import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class TestJDBC3 {
    public static void main(String[] args) throws SQLException {
//        1.创建DataSource
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java101?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("109614");

//        2.建立数据库连接
        Connection connection = dataSource.getConnection();

//        3.拼装sql
//        删除一条记录,删除用户输入id对应的学生记录
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的学生id:");
        int id = scanner.nextInt();
        String sql = "delete from student where id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        //PreparedStatement对象,能够帮助我们动态的构造出sql,同时也进行一些校验检查
        statement.setInt(1,id);

        //4.执行sql
       int ret = statement.executeUpdate();
        System.out.println("ret = "+ret);
        //5.释放相关资源
        statement.close();
        connection.close();
    }
}
